<?php
/*
* @desc 支付控制类
* @author kevin.wang
* @date 2015-01-01 12:00:00
*/
class PayController extends Controller
{
	//过滤器
	public function filters()
    {
        return array(
            'UserLogin',
        );
    }
	//支付首页
	public function actionIndex()
	{
		$request = Yii::app()->request;
		$goodsSn = $request->getParam('goodssn');
		$goods = new Goods();
		$produce = $goods->getGoodsDetail($goodsSn);
		if(empty($produce)){
			throw new ExceptionClass("商品未找到");
		}
		$cookies = $request->getCookies();
		$personInfo = json_decode($cookies[$goodsSn]->value, true);
		if(empty($personInfo)){
			throw new ExceptionClass("获取用户信息失败");
		}
		$this->render('index', array(
				"produce" => $produce,
				"personInfo" => $personInfo
			));
	}
	//订单预定接口
	public function actionBook(){
		$request = Yii::app();
		$data = array('status' => 0, 'body' => array());
		if(!$request->request->isAjaxRequest){
			$data['status'] = 10002;
			$this->renderJson($data);
		}
		//旅客信息校验
		$info = $request->request->getParam("info");
		$passenger = new PassengerForm;
		if(!$passenger->validateForm($info)){
			$msg = array_values($passenger->getErrors());
			$data['status'] = 10000;
			$data['msg'] = $msg[0];
			$this->renderJson($data);
		}
		//商品信息校验
		$order = $request->request->getParam("order");
		$orderForm = new OrderForm;
		if(!$orderForm->validateForm($order)){
			$msg = array_values($orderForm->getErrors());
			$data['status'] = 10000;
			$data['msg'] = $msg[0];
			$this->renderJson($data);
		}
		//订单数据入库
		$orderDetailData = $orderForm->getOrderData();
		$orderData = $orderForm->mainOrderData();
		$passengerData = $passenger->getPassengerData($orderData['order_sn']);		
		$orderModel = new Order;
		if(!$orderModel->insertOrder($passengerData, $orderData, $orderDetailData)){
			$data['status'] = 10004;
			$this->renderJson($data);
		}
		//成功输出
		$data['body']['returnUrl'] = Yii::app()->createUrl('pay/confirm', array('ordersn' => $orderData['order_sn']));
		$this->renderJson($data);
	}
	//支付确认页面
	public function actionConfirm()
	{
		$request = Yii::app();
		$orderSn = $request->request->getParam("ordersn");
		if(empty($orderSn)){
			throw new ExceptionClass("订单号不能为空");
		}
		$userInfo = json_decode(Yii::app()->session['userinfo'], true);
		$model = new Order;
		$mainOrder = $model->getOrderInfo($orderSn, $userInfo['user_id']);
		if(empty($mainOrder)){
			throw new ExceptionClass("未找到订单");
		}
		$orderDetail = $model->getOrderDetail($orderSn);
		if(empty($orderDetail)){
			throw new ExceptionClass("未找到订单详情");
		}
		$this->render('confirm', array(
				'mainOrder'=>$mainOrder,
				'orderDetail'=>$orderDetail
			));
	}
	//支付结果页面
	public function actionPayresult()
	{
		$request = Yii::app();
		$orderSn = $request->request->getParam("ordersn");
		if(empty($orderSn)){
			throw new ExceptionClass("订单号不能为空");
		}
		$userInfo = json_decode(Yii::app()->session['userinfo'], true);
		$model = new Order;
		$mainOrder = $model->getOrderMain($orderSn, $userInfo['user_id']);
		if(!in_array($mainOrder['pay_status'], array(2,3))){
			throw new ExceptionClass("订单尚未支付");
		}
		$this->layout = '//layouts/base';
		$this->render('payresult', array(
				'mainOrder' => $mainOrder
			));
	}
}
